package the.broccoli.market.niche.interfaces.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.market.niche.infrastructure.common.enums.NicheContentTypeEnum;
import the.broccoli.market.niche.interfaces.controller.req.*;
import the.broccoli.market.niche.interfaces.controller.resp.NicheContentItemResp;
import the.broccoli.market.niche.service.NicheContentService;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "Niche")
@RestController
@RequestMapping("/niche/content")
public class NicheContentController {

    @Resource
    private NicheContentService nicheContentService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody @Valid NicheContentAddReq req) {
        req.setType(NicheContentTypeEnum.NOTICE.getCode());
        return APIResult.success(nicheContentService.add(req));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody @Valid NicheContentModifyReq req) {
        return APIResult.success(nicheContentService.modify(req));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<NicheContentItemResp>> page(NicheContentPageReq req) {
        return APIResult.success(nicheContentService.page(req));
    }

    @GetMapping("/get")
    public APIResult<NicheContentItemResp> get(@RequestParam("id") Integer id){
        return APIResult.success(nicheContentService.get(id));
    }

    @PostMapping("/open")
    public APIResult<Boolean> open(@RequestBody @Valid NicheContentOpenReq req) {
        return APIResult.success(nicheContentService.open(req));
    }

    @GetMapping("/current")
    public APIResult<NicheContentItemResp> getCurrent(@RequestParam("positionId") Integer positionId) {
        NicheContentCurrentReq req = new NicheContentCurrentReq();
        req.setPositionId(positionId);
        return APIResult.success(nicheContentService.getCurrent(req));
    }
}
